package com.chaber.containers.views;

import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

import com.chaber.containers.beans.ProductData;

public class ProdCatAddModRemView extends AbstractAddModRemView {

	// fields
	private TextField codeField;
	private TextField nameField;
	private Label parentCatField;
	private Label cdField;
	private Label mdField;
	
	private TableView<ProductData> productsTable = new TableView<>();
	private VBox tableBox;

	public ProdCatAddModRemView(String title) {
		super(title);
		
		HBox actionButtonsBox = this.buildActionButtons();
		this.tableBox = this.buildTableBox();
		
		VBox buttonsAndTableBox = new VBox();
		buttonsAndTableBox.getChildren().addAll(actionButtonsBox, tableBox);
		BorderPane.setAlignment(actionButtonsBox, Pos.TOP_CENTER);
		this.contentPane.setBottom(buttonsAndTableBox);
	}

	@Override
	public void activateEdition() {
		this.codeField.setDisable(false);
		this.nameField.setDisable(false);
		this.tableBox.setVisible(false);
		
		this.initModifyButton.setDisable(true);
		this.removeButton.setDisable(true);
		this.saveButton.setDisable(false);
	}

	@Override
	public void deactivateEdition() {
		this.codeField.setDisable(true);
		this.nameField.setDisable(true);
		this.tableBox.setVisible(true);

		this.initModifyButton.setDisable(false);
		this.removeButton.setDisable(false);
		this.saveButton.setDisable(true);
	}

	@Override
	public void clearFields() {
		this.errorsBox.getChildren().clear();
		
		this.codeField.setText(null);
		this.nameField.setText(null);
		this.cdField.setText(null);
		this.mdField.setText(null);
		this.productsTable.getItems().clear();
	}

	@Override
	protected Node buildDetailsFields() {
		GridPane detailsFieldsGrid = new GridPane();
		detailsFieldsGrid.setVgap(20);
		detailsFieldsGrid.setHgap(10);

		Label codeLabel = new Label("Code:");
		this.codeField = new TextField();
		detailsFieldsGrid.add(codeLabel, 0, 0);
		detailsFieldsGrid.add(this.codeField, 1, 0);

		Label nameLabel = new Label("Name:");
		this.nameField = new TextField();
		detailsFieldsGrid.add(nameLabel, 0, 1);
		detailsFieldsGrid.add(this.nameField, 1, 1);

		Label parentCatLabel = new Label("Parent category:");
		this.parentCatField = new Label();
		detailsFieldsGrid.add(parentCatLabel, 0, 2);
		detailsFieldsGrid.add(this.parentCatField, 1, 2);

		Label cdLabel = new Label("Creation date:");
		this.cdField = new Label();
		detailsFieldsGrid.add(cdLabel, 0, 3);
		detailsFieldsGrid.add(this.cdField, 1, 3);

		Label mdLabel = new Label("Modification date:");
		this.mdField = new Label();
		detailsFieldsGrid.add(mdLabel, 0, 4);
		detailsFieldsGrid.add(this.mdField, 1, 4);

		detailsFieldsGrid.setAlignment(Pos.CENTER);

		return detailsFieldsGrid;
	}
	
	private VBox buildTableBox() {
		VBox tableBox = new VBox();
		
		Label assignedProductsLabel = new Label("Assigned products");
		assignedProductsLabel.setFont(new Font("Arial", 20));
		this.productsTable = this.buildProductsTable();
		
		tableBox.getChildren().addAll(assignedProductsLabel, this.productsTable);
		
		return tableBox;
	}
	
	private TableView<ProductData> buildProductsTable() {
		TableView<ProductData> productsTable = new TableView<>();

		TableColumn<ProductData, String> barcodeColumn = new TableColumn<>("Barcode");
		barcodeColumn.setCellValueFactory(new PropertyValueFactory<ProductData, String>("barCode"));

		TableColumn<ProductData, String> nameColumn = new TableColumn<>("Name");
		nameColumn.setCellValueFactory(new PropertyValueFactory<ProductData, String>("name"));

		TableColumn<ProductData, Integer> amountColumn = new TableColumn<>("Amount");
		amountColumn.setCellValueFactory(new PropertyValueFactory<ProductData, Integer>("amount"));
		
		productsTable.getColumns().add(barcodeColumn);
		productsTable.getColumns().add(nameColumn);
		productsTable.getColumns().add(amountColumn);

		productsTable.setEditable(false);
		productsTable.setMaxHeight(100);
		productsTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
		
		return productsTable;
	}

	public TextField getNameField() {
		return nameField;
	}

	public void setNameField(TextField nameField) {
		this.nameField = nameField;
	}

	public Label getCdField() {
		return cdField;
	}

	public void setCdField(Label cdField) {
		this.cdField = cdField;
	}

	public Label getMdField() {
		return mdField;
	}

	public void setMdField(Label mdField) {
		this.mdField = mdField;
	}

	public TextField getCodeField() {
		return codeField;
	}

	public void setCodeField(TextField codeField) {
		this.codeField = codeField;
	}

	public Label getParentCatField() {
		return parentCatField;
	}

	public void setParentCatField(Label parentCatField) {
		this.parentCatField = parentCatField;
	}

	public TableView<ProductData> getProductsTable() {
		return productsTable;
	}

	public void setProductsTable(TableView<ProductData> productsTable) {
		this.productsTable = productsTable;
	}
}
